<?php
use yii\helpers\Html;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $content string 字符串 */

$web = Url::to("@web");

?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>产品搜索</title>
    <link type="text/css" href="<?= $web ?>/css/wx-good/goods-list.css" rel="stylesheet"/>
    <link type="text/css" href="<?= $web ?>/css/wx-good/global.css" rel="stylesheet"/>
    <style type="text/css">

        .middle-search {
            background-image: url("<?= $web ?>/img/wx-good/welfare-platform-1.png");
            background-size: 100% 100%;
        }
    </style>

    <script type="text/javascript" src="<?= $web ?>/js/jquery.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/tools.js"></script>
    <?php require_once Yii::$app->viewPath . '/wx-no-share.php' ?>
    <script type="text/javascript">
        $(function () {
            // 任何需要执行的js特效
            var w = $(document).width();
            if (w > 640) {
                w = 640;
            }
            $("html").css("font-size", w * 20 / 320);
        });

        $(document).scroll(function () {
            var scrollTop = $(document).scrollTop();
            var scrollHeight = $(window).height();
            var contentHeight = $(document).height();
            if (scrollTop + scrollHeight >= contentHeight) {
                isScroll = true;
                loadGoodsList(false);
            }
        });


        var start = 0;
        var pagesize = 6;
        var good_tpl = "";
        var list_isClear = false;
        var isScroll = false;
        $(function ($) {
            goods_tpl = returnstring("<?=$web?>/js/wx-good/goods-list-goods.tpl");
            if ('<?=$category?>' != '0') {
                loadGoodsList(true);
            }

        });
        function href_goods_detail(goods_code) {
            goods_code = encodeURI(goods_code);
            window.location.href = "<?=Url::to(["wx-good/goods-detail"]);?>?goods_code=" + goods_code;
        }
        var isloading = false;
        function loadGoodsList(isClear) {
            if (isloading) {
                return;
            }
            if (isScroll) {
                $(".wait-bottom").show();
            }
            else {
                $(".wait-top").show();
            }

            list_isClear = isClear;
            var data = {};
            data.start = start;
            data.pagesize = pagesize;
            data.category = '<?=$category?>';
            data.filter = '';
            var f = $('.input_text').val()._trim();
            if (f == '') {
                data.filter = 0;
            } else {
                data.filter = f;
            }
            isloading = true;
            requestJson("<?=Url::to(['wx-good/get-goods-list-handle'])?>", data, true, loadGoodsSuccess);
        }

        function loadGoodsSuccess(response) {
            $(".wait-top").hide();
            $(".wait-bottom").hide();
            if (response.status == 0) {
                var target = $(".commodity-search");
                if (list_isClear) {
                    target.empty();
                    start = 0;
                }
                $.each(response.data.goodsList, function (i, item) {
                    var dom = document.createElement("DIV");
                    dom.innerHTML = goods_tpl._format({
                        code: item.GOODS_CODE,
                        type: item.TYPE,
                        name: item.GOODS_NAME,
                        pic: item.image_url == '' ? "<?= $web ?>/img/goods-default.jpg" : item.GOODS_IMG_URL,
                        price: item.PRICE,
                        action: item.TYPE == '1' ? '添加购物车' : '直接购买'
                    });
                    target.get(0).appendChild(dom);
                    $(".commodity-integral img").attr('src', '<?= $web ?>/img/wx-good/commodity-1.png');
                    $(".commodity-car img").attr('src', '<?= $web ?>/img/wx-good/commodity-2.png');
                });
                start = start + response.data.goodsList.length;
                isloading = false;
            } else {
                alert(response.message);
            }
        }

        function btnSearchClick() {
            isScroll = false;
            loadGoodsList(true);
        }

        function clearSearchText() {
            $(".input_text").val("");
        }

        function addShoppingCart(goods_code, type) {
            if (type == 1) {
                $("#loading_toast").show();

                var data = {};
                data.goods_code = goods_code;
                requestJson('<?=Url::to(['wx-good/add-goods-to-cart-handle'])?>', data, true, function (response) {
                    $("#loading_toast").hide();

                    if (response.status == 0) {
                        if (response.data.TOTALNUM > 99) {
                            $('#v_TOTALNUM').html('99+');
                        } else {
                            $('#v_TOTALNUM').html(response.data.TOTALNUM);
                        }
                        $("#confirm_dialog").show();
                    } else {
                        alert(response.message);
                    }
                });
            } else if (type == 2) {
                window.location.href = "<?=Url::to(["wx-good/create-virtual-goods-to-order-handle"]);?>?goods_code=" + goods_code;
            }
        }

        function goto_cart() {
            window.location.href = "<?=Url::to(["wx-good/shopping-cart"]);?>";
        }

        function cancle_confirm() {
            $("#confirm_dialog").hide();
        }
    </script>
</head>
<body style="background-color:#ebebeb; ">
<div class="main" style="width: 16rem; margin: 0 auto;">
    <div class="search-head">
        <div class="search-search">
            <img src="<?= $web ?>/img/wx-good/search-1.png" style="width: 0.8rem; height: 0.8rem;
              float: left; margin-left: 0.3rem; margin-top: 0.5rem;
              ">
            <input class="input_text" type="text" placeholder="<?= $category == '0' ? '商品名称' : $category ?>" style="width: 8.6rem; height: 1rem;
              float: left; margin-left: 0.3rem; margin-top: 0.4rem; border: none; font-size: 0.7rem; outline: none;
              ">
            <img src="<?= $web ?>/img/wx-good/search-2.png" onclick="clearSearchText()" style="width: 0.8rem; height: 0.8rem;
              float: left; margin-left: 0.8rem; margin-top: 0.5rem;
              ">
        </div>
        <div class="search-button" onclick="btnSearchClick();">
            搜索
        </div>
    </div>
    <div class="wait-top">
        <!-- loading toast -->
        <div id="loading_toast" class="weui_loading_toast">
            <div class="weui_mask_transparent"></div>
            <div class="weui_toast">
                <div class="weui_loading">
                    <div class="weui_loading_leaf weui_loading_leaf_0"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_1"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_2"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_3"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_4"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_5"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_6"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_7"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_8"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_9"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_10"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_11"></div>
                </div>
                <p class="weui_toast_content">数据加载中</p>
            </div>
        </div>
        <!-- end loading toast -->
    </div>
    <div class="commodity-search">
        <!--<div class="commodity">
            <div class="commodity-img">
                <img src="imgs/order-5.png" style="width: 3rem; height: 3rem;">
            </div>
            <div class="commodity-right">
                <div class="commodity-name">
                    雅图银兴梦银兴梦银兴梦空间国际影院电影票XXXXXXxxxxxxxXXXXXXXXXXXXXXXXXXXXXXX
                </div>
                <div class="commodity-label">
                    <div class="commodity-integral">积分&nbsp;<span>10.5</span></div>
                    <div class="commodity-car">
                        添加到购物车
                    </div>
                </div>
            </div>
        </div>-->
    </div>
    <div class="wait-bottom">
        <!-- loading toast -->
        <div id="loading_toast" class="weui_loading_toast">
            <div class="weui_mask_transparent"></div>
            <div class="weui_toast">
                <div class="weui_loading">
                    <div class="weui_loading_leaf weui_loading_leaf_0"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_1"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_2"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_3"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_4"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_5"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_6"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_7"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_8"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_9"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_10"></div>
                    <div class="weui_loading_leaf weui_loading_leaf_11"></div>
                </div>
                <p class="weui_toast_content">数据加载中</p>
            </div>
        </div>
        <!-- end loading toast -->
    </div>
</div>
<!--BEGIN dialog confirm-->
<div id="confirm_dialog" class="weui_dialog_confirm" style="display: none;">
    <div class="weui_mask"></div>
    <div class="weui_dialog">
        <div class="weui_dialog_hd">
            <div class="weui_dialog_title">添加购物车成功，是否需要进入购物车?</div>
        </div>
        <div class="weui_dialog_bd">
        </div>
        <div class="weui_dialog_ft">
            <a href="javascript:;" class="weui_btn_dialog default" onclick="cancle_confirm();">取消</a>
            <a href="javascript:;" class="weui_btn_dialog primary" onclick="goto_cart();">确定</a>
        </div>
    </div>
</div>
<!--END dialog confirm-->
</body>

</html>

